/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

import { CommandDialog, pickedLineData } from '@amcax/base';
import { CommandFormWrapper } from '@app-cad/common/components/command-form';
import { PickInputForFocus } from '@app-cad/common/components/command-form/auto-focus/pick-input';
import { CommandFormFooter } from '@app-cad/common/components/command-form/footer';
import { PickInputValueType } from '@app-cad/common/components/pick-input/3d';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { useTools } from '@app-cad/common/hooks/useTools';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { useAsyncEffect } from 'ahooks';
import i18n from 'i18next';
import { useState } from 'react';

type FormData = {
  edge?: pickedLineData;
};

type Params = {
  edge?: string;
};

function CommandSurfaceUntrim({
  title,
  onClose,
  props,
}: CurvedCmdDialogParams) {
  const [formData, setFormData] = useState<FormData>({});

  const { picker } = useTools();

  useAsyncEffect(async () => {
    if (props == null) {
      setFormData({});
      return;
    }
    const params: Params = props.params;
    const { edge } = params;

    await initEditDependency([edge]);

    setFormData({
      edge: picker.generatePickObjByPickID(edge) as pickedLineData,
    });
  }, [props?.shape]);

  const isValid = () => formData.edge != null;

  const checkAndFinishDraw = async () => {
    if (!isValid()) {
      return;
    }

    await execute(false);

    onClose();
  };

  const { initEditDependency, clear, confirmCommand, isLoading, initingEdit } =
    useCommand('cmd-surface-trim', {
      commandName: 'CreateSurface',
      editDxid: props?.shape,
      onClose,
      finishDraw: checkAndFinishDraw,
    });

  const execute = (preview: boolean) => {
    return confirmCommand(
      {
        type: 'Untrim',
        a: {
          edge: formData.edge.edgeId,
        },
      },
      preview,
    );
  };

  const preview = async () => {
    if (!isValid()) {
      clear();
      return;
    }

    await execute(true);
  };

  useAsyncEffect(preview, [formData]);

  return (
    <CommandDialog
      title={title}
      onClose={onClose}
      placement='topLeft'
    >
      <CommandFormWrapper>
        <PickInputForFocus
          autoFocus
          allowPickType={[PickInputValueType.Curve]}
          onPick={([result]) => {
            setFormData({
              edge: result,
            });
          }}
          value={[formData.edge]}
          placeholder='请输入边界'
          disabled={initingEdit}
        />
        <CommandFormFooter
          onClose={onClose}
          onSubmit={() => {
            checkAndFinishDraw();
          }}
          submitText='完成'
          isSubmitLoading={isLoading}
          isSubmitDisabled={!isValid()}
          isPreviewDisabled={!isValid()}
        />
      </CommandFormWrapper>
    </CommandDialog>
  );
}

export const cmdSurfaceUntrim: CurvedBaseCommand = {
  id: 'CommandSurfaceUntrim',
  label: 'command.surface-untrim',
  icon: 'commands/curved/untrim-surface',
  create: (label, onClose, props) => (
    <CommandSurfaceUntrim
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
